//app.js
var utils = require("utils/utils.js");
App({
  onShow:function(e){
    var that = this;
    if (utils.is_define(e.referrerInfo.extraData)) {
      wx.getSystemInfo({
        success(res) {
          console.log(res.platform)
          if (res.platform == 'android') {
            let data = JSON.parse(e.referrerInfo.extraData.finalCallbackData);
            let path = decodeURIComponent(data.path);
            let codeJson = JSON.parse(path.split("param=")[1]);
            let code = codeJson.code;
            that.globalData.wpscode = code
          } else {
            var str = JSON.parse(e.referrerInfo.extraData.finalCallbackData)
            //  wx.showModal({
            //   title: '1',
            //   content:str.code,
            //   cancelColor: 'cancelColor',
            // })
            that.globalData.wpscode = str.code
          }
        }
      })

    }
  },
  isLowerThenVersion(target) {
    let SDKVersion = wx.getSystemInfoSync().SDKVersion;
    let currArr = /([\d]{1,}).([\d]{1,}).([\d]{1,})/.exec(SDKVersion);
    let targArr = /([\d]{1,}).([\d]{1,}).([\d]{1,})/.exec(target);
    let currMaj = +currArr[1]
    let currMin = +currArr[2]
    let currPat = +currArr[3]
    let targMaj = +targArr[1]
    let targMin = +targArr[2]
    let targPat = +targArr[3]
    if (currMaj < targMaj) return true;
    if (currMaj > targMaj) return false;
    if (currMin < targMin) return true;
    if (currMin > targMin) return false;
    if (currPat < targPat) return true;
    return false
  },
  onLaunch: function (e) {
    this.uploadMam()
    var that = this.globalData;
    var menuButtonObject = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: (result) => {
        var statusBarHeight = result.statusBarHeight;
        var navTop = menuButtonObject.top;//胶囊按钮距离顶部信息
        var navHeight = statusBarHeight + menuButtonObject.height + (navTop - statusBarHeight) * 2;//导航栏高度
        var menuButtonHeight = menuButtonObject.height;
        that.ComponentsNavTop = navTop + (menuButtonHeight - 9)
        that.navTop = navTop;
        that.navHeight = navHeight;
        that.windowHeight = result.windowHeight;
        that.menuButtonHeight = menuButtonHeight;
      },
    })

  },
  onHide(){
    
  },
  uploadMam(){
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      if (res.hasUpdate) {
        updateManager.onUpdateReady(function () {
          wx.showModal({
            title: '更新提示',
            content: '新版本已经准备好，是否重启应用？',
            success: function (res) {
              if (res.confirm) {
                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                updateManager.applyUpdate()
              }
            }
          })
        })
        updateManager.onUpdateFailed(function () {
          // 新的版本下载失败
          wx.showModal({
            title: '已经有新版本了哟~',
            content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
          })
        })
      }
    })
  },
  get_local(key){
    return new Promise((resolve, reject) => {
      wx.getStorage({
        key:key,
        success (res) {
          resolve(res.data)
        },fail(){
          resolve('')
        }
      })
    })
  },
  //获取打印机是否支持
  isSupport(code){
    return new Promise((resolve, reject) => {
      utils.request('Order/checkService',{'code':code}, function (res) {
        if(res.data.code==1){
          wx.setStorageSync('scale',res.data.picRate)
        }else{
          wx.setStorageSync('scale',4)
        }
        resolve(res.data.code)
      })
    })
  },
    // 是否设置支付密码
    isSetPassWord(callback){
      utils.request('user/userInfo',{}, function (res) {
          if(res.data.code==200){
            if(res.data.data.isSetPayPwd!=1){
              wx.showModal({
                title: '提示',
                content: '您还未设置支付密码，现在去设置吗？',
                confirmText:'去设置',
                success (res) {
                  if (res.confirm) {
                      wx.navigateTo({
                        url: '/pages/my/password/password',
                      })
                  }
                }
              })
              return false;
            }else{
              callback();
            }
          }else{
            utils.toast(res.data.msg);
            return false;
          }
      })
    },
  globalData: {
    userInfo: null,
    navHeight:'',
    navTop:'',
    windowHeight:'',
    menuButtonHeight:'',
    ComponentsNavTop:'',
    constUrl:'https://service.zv100.net'
  }
})